package com.jiazhong.mingxing.boot.boot14.controller;

import com.alibaba.fastjson.JSONArray;
import com.jiazhong.mingxing.boot.boot14.bean.Order;
import jakarta.annotation.Resource;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private RocketMQTemplate rocketMqTemplate;

    @RequestMapping("/add")
    public String add(Order order) {
        // 1. 消息的发送者
        Message<String> message = MessageBuilder.withPayload(JSONArray.toJSONString(order))
                .setHeader("KEYS", order.getOrderId().toString())
                .build();
        rocketMqTemplate.sendMessageInTransaction("TopicB:transaction1", message, JSONArray.toJSONString(order));
        return "success";
    }
}
